/**
 * 构建日期时间字符串
 * @param {Date} date
 * @returns
 */
const formattedDate = (date) => {
  // 如果你想以特定格式输出这个日期，可以手动构建字符串
  var year = date.getFullYear();
  var month = ("0" + (date.getMonth() + 1)).slice(-2); // 月份从 0 开始，所以需要加 1
  var day = ("0" + date.getDate()).slice(-2);
  var hours = ("0" + date.getHours()).slice(-2);
  var minutes = ("0" + date.getMinutes()).slice(-2);
  var seconds = ("0" + date.getSeconds()).slice(-2);
  // 构建日期时间字符串
  return (
    year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds
  );
};

/**
 * @param {string} str 例 2024-11-01T06:30:00
 */
const main = (str) => {
  try {
    const date = new Date(str);
    // 开始时间毫秒数
    const millisecond = date.getTime();
    // 1h
    const unit = 60 * 60 * 1000;
    // 12h
    date.setTime(millisecond + 12 * unit);
    console.log("12h", formattedDate(date));
    // 1d
    date.setTime(millisecond + 24 * unit);
    console.log("1d", formattedDate(date));
    // 2d
    date.setTime(millisecond + 2 * 24 * unit);
    console.log("2d", formattedDate(date));
    // 4d
    date.setTime(millisecond + 4 * 24 * unit);
    console.log("4d", formattedDate(date));
    // 7d
    date.setTime(millisecond + 7 * 24 * unit);
    console.log("7d", formattedDate(date));
    // 15d
    date.setTime(millisecond + 15 * 24 * unit);
    console.log("15d", formattedDate(date));
    // 1m
    date.setTime(millisecond + 30 * 24 * unit);
    console.log("1m", formattedDate(date));
    // 3m
    date.setTime(millisecond + 3 * 30 * 24 * unit);
    console.log("3m", formattedDate(date));
    // 6m
    date.setTime(millisecond + 6 * 30 * 24 * unit);
    console.log("6m", formattedDate(date));
  } catch (error) {
    console.log(error);
  }
};
main("2024-11-24T06:30:00");
